package com.autoswf.dispatcher
{
	import com.autoswf.core.Transport;
	
	import flash.events.Event;
	import flash.events.MouseEvent;
	
	import flexunit.framework.TestCase;

	public class TypedDispatcherTest extends TestCase
	{
		private var dispatcher:TypedDispatcher;
		
		public function TypedDispatcherTest(methodName:String=null)
		{
			super(methodName);
		}
		
		override public function setUp():void
		{
			dispatcher = new TypedDispatcher("link");
		}
		
		
		/** Tests that a type safe event listener checks string and class type */		
		public function test01():void
		{
			var handler:Function = function(event:Event):void
			{
				assertTrue(event is Event);
			}
			
			dispatcher.addTypedListener(Event, Event.CHANGE, addAsync(handler, 20));
			dispatcher.sendUp(new Transport( new Event(Event.CHANGE) ));
		}
		
		
		/** Tests that a type safe event listener ignores string matches with wrong class typing */
		public function test02():void
		{
			var handler:Function = function(event:Event):void
			{
				assertNull("TypedDispatcherTest.test02()", event);
			}
			
			dispatcher.addTypedListener(Event, Event.CHANGE, handler);
			dispatcher.sendUp(new Transport( new MouseEvent(Event.CHANGE) ));
		}
		
		
		/** Tests that a listener typed to a super class doesnt fire with subclass and matching string type */
		public function test03():void
		{
			var handler:Function = function(event:Event):void
			{
				assertNull("TypedDispatcherTest.test03()", event);
			}
			
			dispatcher.addTypedListener(MouseEvent, MouseEvent.CLICK, handler);
			dispatcher.sendUp(new Transport( new Event(MouseEvent.CLICK) ));
		}

		/** Tests that a type safe event listener is removed */		
		public function test04():void
		{
			var handler:Function = function(event:Event):void
			{
				assertNull("TypedDispatcherTest.test04()", event);
			}
			
			dispatcher.addTypedListener(Event, Event.CHANGE, handler);
			dispatcher.removeTypedListener(Event, Event.CHANGE, handler);
			dispatcher.sendUp(new Transport( new Event(Event.CHANGE) ));
		}

		/** Tests persistence of specific dispatchers */		
		public function test05():void
		{
			var handler:Function = function(event:Event):void
			{
			}
			
			dispatcher.addTypedListener(Event, Event.CHANGE, handler);
			dispatcher.removeTypedListener(Event, Event.CHANGE, handler);
			dispatcher.addTypedListener(Event, Event.CHANGE, addAsync(handler, 20));
			dispatcher.sendUp(new Transport( new Event(Event.CHANGE) ));
		}
		
	}
}